package com.mocoop.plugin.db;

import java.util.List;
import java.util.Map;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.json.JSONArray;
import org.json.JSONException;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

import com.google.gson.Gson;
import com.mocoop.commom.MyConstants;
import com.mocoop.dao.SyncDaoImp;
import com.mocoop.plugin.MocoopPlugin;

public class DBHelper extends MocoopPlugin {

	@Override
	public boolean execute(final String action, final CordovaArgs args, final CallbackContext callbackContext) throws JSONException {

		cordova.getThreadPool().execute(new Runnable() {
			SyncDaoImp dao = new SyncDaoImp(getActivity(), getUserID());
			@Override
			public void run() {
				try {
					

//					Log.d("RZZ", "开始执行sql" + args.getString(0) + System.currentTimeMillis());
					if("findDB".equals(action)){
						JSONArray jr = (JSONArray) args.get(1);
						String[] arr = new String[jr.length()];
						for (int i = 0; i < jr.length(); i++) {
							arr[i] = jr.getString(i);
						}
						List<Map<String, String>> list = dao.execute(args.getString(0), arr);
//						Log.d("RZZ", "sql执行结束" + args.getString(0) + System.currentTimeMillis());
						callbackContext.success(new Gson().toJson(list));
					}else if ("saveToSharedPreferences".equals(action)){
						String dbName;
						if("public".equals(args.getString(0))){
							  dbName = "html";
						}else if("private".equals(args.getString(0))){
							 dbName =  "html"+getUserID(); 
						}else{
							return;
						}
						SharedPreferences sharedPreferences = getActivity().getSharedPreferences(dbName, 0);   

					     Editor editor = sharedPreferences.edit(); 
					     editor.putString(args.getString(1),args.getString(2));
						 editor.commit();
						callbackContext.success();
					}else if("findSharedPreferences".equals(action)){
						String dbName;
						if("public".equals(args.getString(0))){
							  dbName = "html";
						}else if("private".equals(args.getString(0))){
							 dbName =  "html"+getUserID(); 
						}else{
							return;
						}
						SharedPreferences sharedPreferences = getActivity().getSharedPreferences(dbName, 0);   
						String value = sharedPreferences.getString(args.getString(1), "NULL");
						callbackContext.success(value);
					}else if("deleteSharedPreferences".equals(action)){
						String dbName;
						if("public".equals(args.getString(0))){
							  dbName = "html";
						}else if("private".equals(args.getString(0))){
							 dbName =  "html"+getUserID(); 
						}else{
							return;
						}
						SharedPreferences sharedPreferences = getActivity().getSharedPreferences(dbName, 0);   
						
						 Editor editor = sharedPreferences.edit(); 
					     editor.remove(args.getString(1));
						 editor.commit();
					}else {
						JSONArray jr = (JSONArray) args.get(1);
						String[] arr = new String[jr.length()];
						for (int i = 0; i < jr.length(); i++) {
							arr[i] = jr.getString(i);
						}
						List<Map<String, String>> list = dao.execute(args.getString(0), arr);
//						Log.d("RZZ", "sql执行结束" + args.getString(0) + System.currentTimeMillis());
						callbackContext.success(new Gson().toJson(list));
						
					}

				} catch (Exception e) {
				}
			}
		});
		return true;
	}
}
